/**
 * @LogConsole.as
 * 
 * @author sodaChen mail:asframe@qq.com
 * @version 1.0
 * <br>Copyright (C), 2010-2016 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame Log
 * <br>Date:2011-9-10
 */
package com.asframe.log.console
{
	import com.asframe.collections.maps.HashMap;
	import com.asframe.log.ILogConsole;
	import com.asframe.log.Level;
	import com.asframe.log.LogFormat;
	import com.asframe.log.format.IObjectFormat;
	import com.asframe.utils.ClassUtils;
	import com.asframe.utils.FormatTool;
	
	/**
	 * ASFrame自己的默认实现
	 * @author sodaChen
	 * Date:2011-9-10
	 */
	public class LogConsole implements ILogConsole
	{
//		/** 对象格式处理集合 **/
//		protected var headFormat:ILogHeadFormat;
		/** 对象格式处理集合 **/
		protected var logOutput:ILogConsole;
		protected var showTime:Boolean;
		/** 对象格式处理集合 **/
		protected var objFormatMap:HashMap;
		protected var logFormat:LogFormat;
		protected var infos:String;
		public function LogConsole()
		{
			objFormatMap = new HashMap();
			logFormat = new LogFormat();
			logFormat.setObjectFormatMap(objFormatMap);
		}
		/**
		 * 是否显示时间 
		 * @param showTime
		 * @param dateFormat
		 */		
		public function isShowTime(showTime:Boolean,dateFormat:String = null):void
		{
			this.showTime = showTime;
		}
//		/**
//		 * 日志头的处理 
//		 * @param headFormat
//		 */		
//		public function setHeadFormat(headFormat:ILogHeadFormat):void
//		{
//			
//		}
		/**
		 * 添加一个对象处理格式 
		 * @param objectFormat：对象处理格式接口
		 */		
		public function addObjectFormat(clazz:Class,objectFormat:IObjectFormat):void
		{
			objectFormat.setLogFormat(logFormat);
			objFormatMap.put(ClassUtils.getFullyQualifiedName(clazz),objectFormat);
		}
		public function console(level:Level, target:Object, logs:Array):void
		{
			//处理日志消息头
			infos = "[" + level.name + "]";
			if(showTime)
			{
				infos += "[" + FormatTool.timeToHMS(new Date().time) + "]";
			}
			if(target != null)
			{
				//target的目的是方便知道消息是从那里打印的，方便查找
				if(target is String)
				{
					infos += "-[" + target + "]:\n";
				}
				else
				{
//					infos += "-[" + ClassUtils.getFullyNameByObj(target) + "]:";
					infos += "-[" + ClassUtils.getClassName(target) + "]:";
				}
			}
			for (var i:int = 1; i <= logs.length; i++) 
			{
				infos += logFormat.formatObject(logs[i - 1],false);
				if(i < logs.length)
					infos += ",";
			}
			trace(infos);
		}
	}
}